﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace RainyPWS.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:RButton runat=server></{0}:RButton>")]
    public class RButton : WebControl, IPostBackEventHandler
    {

        public RButton():base(HtmlTextWriterTag.A)
        {
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (Page.Header.FindControl("RainyPWS_RButton_CSS") == null)
            {
                string link_location = Page.ClientScript.GetWebResourceUrl(this.GetType(), "RainyPWS.Controls.source.RainyPWS_RButton.css");
                HtmlLink cssLink = new HtmlLink();
                cssLink.Href = link_location;
                cssLink.Attributes.Add("rel", "stylesheet");
                cssLink.Attributes.Add("type", "text/css");
                cssLink.ID = "RainyPWS_RButton_CSS";
                Page.Header.Controls.Add(cssLink);
            }

            if (!Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "RainyPWS_RButton_JS"))
            {
                string jsURL = Page.ClientScript.GetWebResourceUrl(this.GetType(), "RainyPWS.Controls.source.RainyPWS_RButton.js");
                Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "RainyPWS_RButton_JS", jsURL);
            }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "http://www.baidu.com");
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            StringBuilder html = new StringBuilder();
            html.AppendLine(string.Format("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" id=\"rbtn_table_{0}\" style='background-color:#bbb;' class=\"r-btn {1}\">", this.ID, this.CssClass));
            html.AppendLine("<tr>");
            html.AppendLine("<td><i></i></td>");
            html.AppendLine("<td></td>");
            html.AppendLine("<td><i></i></td>");
            html.AppendLine("</tr>");
            html.AppendLine("<tr>");
            html.AppendLine("<td><i></i></td>");
            html.AppendLine(string.Format("<td>{0}</td>", Text));
            html.AppendLine("<td><i></i></td>");
            html.AppendLine("</tr>");
            html.AppendLine("<tr>");
            html.AppendLine("<td><i></i></td>");
            html.AppendLine("<td></td>");
            html.AppendLine("<td><i></i></td>");
            html.AppendLine("</tr>");
            html.AppendLine("</table>");
            output.BeginRender();
            output.Write(html.ToString());
            output.EndRender();
        }

        #region Implement "IPostBackEventHandler"
        public event EventHandler Click;

        protected virtual void OnClick(EventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            OnClick(new EventArgs());
        }
        #endregion
    }
}
